package com.security.config;

import com.alibaba.fastjson.JSON;
import com.common.response.ResponseBean;
import com.common.response.ResponseCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/** 
* @Description:  权限不足处理类，返回403
* @Author: zyj 
* @Date: 2020/1/3 
*/
@Component("RestAuthenticationAccessDeniedHandler")
public class RestAuthenticationAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        //登陆状态下，权限不足执行该方法
        System.out.println("权限不足：" + e.getMessage());
        response.setStatus(200);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter printWriter = response.getWriter();

        ResponseBean responseBean = new ResponseBean();
        responseBean.setCode(ResponseCode.FORBIDDEN.getValue());
        responseBean.setMessage(ResponseCode.FORBIDDEN.getDescripe());
        printWriter.write(JSON.toJSONString(responseBean));
        printWriter.flush();
    }
}
